<?php

namespace Modules\System\Services\Agora;

use DateTime;
use DateTimeZone;
use Illuminate\Support\Facades\Validator;
use Modules\System\Models\ConfigModel;
use Modules\System\Services\Mds\MDSCurl;
use RtcTokenBuilder;
use RtmTokenBuilder;

class ShengWangServices
{

    static $appID = "3f1bd1609557441e8823818edc9aec2b";                      //appID
    static $appCertificate = "ed3b195ca40c4bb9b72942668f874731";             //证书

    static $customerKey = "83d48dc7b34d4940b2fa487d6935de58";                //客户 ID
    static $customerSecret = "1d968478dee2435daf060308d90141bc";             //客户密钥

    public function __construct(){

    }

    static public function channelUserProperty($i){
        //该方法查询指定项目下的频道列表。
        $c = ConfigModel::getKV("ShengWang");
        // 基于 PHP 实现的 Token 认证示例，使用 RTM 的用户事件 RESTful API
        $url = "https://api.agora.io/dev/v1/channel/user/property/".$c['appID']."/".$i['uid']."/".$i['channelName'];

        // 拼接客户 ID 和客户密钥
        $credentials = $c['customerKey'] . ":" . $c['customerSecret'];

        // 使用 base64 进行编码
        $base64Credentials = base64_encode($credentials);
        // 创建 authorization header
        $arr_header = "Authorization: Basic " . $base64Credentials;

        $arr_headerAll = array(
            $arr_header,
            'Content-Type: application/json'
        );

        $response = MDSCurl::getHeader($url,$arr_headerAll);
        return $response;

    }

    static public function channelUser($i){
        //该方法查询指定项目下的频道列表。
        $c = ConfigModel::getKV("ShengWang");
        // 基于 PHP 实现的 Token 认证示例，使用 RTM 的用户事件 RESTful API
        $url = "https://api.agora.io/dev/v1/channel/user/".$c['appID']."/".$i['channelName'];

        // 拼接客户 ID 和客户密钥
        $credentials = $c['customerKey'] . ":" . $c['customerSecret'];

        // 使用 base64 进行编码
        $base64Credentials = base64_encode($credentials);
        // 创建 authorization header
        $arr_header = "Authorization: Basic " . $base64Credentials;

        $arr_headerAll = array(
            $arr_header,
            'Content-Type: application/json'
        );

        $response = MDSCurl::getHeader($url,$arr_headerAll);
        return $response;

    }

    static public function channel($i){
        //该方法查询指定项目下的频道列表。
        $c = ConfigModel::getKV("ShengWang");

        // 基于 PHP 实现的 Token 认证示例，使用 RTM 的用户事件 RESTful API
        $url = "https://api.agora.io/dev/v1/channel/".$c['appID']."?page_no=".$i['page_no']."&page_size=".$i['page_size'];

        // 拼接客户 ID 和客户密钥
        $credentials = $c['customerKey'] . ":" . $c['customerSecret'];

        // 使用 base64 进行编码
        $base64Credentials = base64_encode($credentials);
        // 创建 authorization header
        $arr_header = "Authorization: Basic " . $base64Credentials;

        $arr_headerAll = array(
            $arr_header,
            'Content-Type: application/json'
        );

        $response = MDSCurl::getHeader($url,$arr_headerAll);
        return $response;

    }

    static public function project($i){
        //创建一个新的 Agora 项目。
    }

    static public function user_events($i){
        // 基于 PHP 实现的 Token 认证示例，使用 RTM 的用户事件 RESTful API

        $c = ConfigModel::getKV("ShengWang");
        $url = "https://api.agora.io/dev/v2/project/".$c['appID']."/rtm/vendor/user_events";
        // RTM Token
        $token = self::RtmTokenBuilder(['uid'=>$i['uid']]);

        // 生成 RTM Token 时使用的 user ID
        $uid = $i['uid'];
        // 在 header 中添加 x-agora-token 字段
        $token_header = "x-agora-token: " . $token;
        // 在 header 中添加 x-agora-uid 字段
        $uid_header = "x-agora-uid: " . $uid;
        $arr_headerAll = array(
            $token_header,
            $uid_header,
            'Content-Type: application/json'
        );
        $response = MDSCurl::getHeader($url,$arr_headerAll);
        return $response;

    }

    static public function projects(){

        $url = "https://api.agora.io/dev/v1/projects";
        $c = ConfigModel::getKV("ShengWang");
        // 拼接客户 ID 和客户密钥
        $credentials = $c['customerKey'] . ":" . $c['customerSecret'];

        // 使用 base64 进行编码
        $base64Credentials = base64_encode($credentials);
        // 创建 authorization header
        $arr_header = "Authorization: Basic " . $base64Credentials;

        $arr_headerAll = array(
            $arr_header,
            'Content-Type: application/json'
        );

        $response = MDSCurl::getHeader($url,$arr_headerAll);

        return $response;

    }

    static public function RtcTokenBuilder($i){
        $c = ConfigModel::getKV("ShengWang");
        $validator = Validator::make(
            $i,
            [
                'channelName' => "required",
                'uid' => "required",
            ],
            [
                'channelName.required' => 'channelName不能为空',
                'uid.required' => 'uid不能为空',
            ]
        );

        if(!empty($validator->errors()->first())){
            mdsResponse(400,$validator->errors()->first());
        }

        include_once(dirname(__FILE__) . "/src/RtcTokenBuilder.php");

        $role = RtcTokenBuilder::RoleAttendee;
        $expireTimeInSeconds = 3600;
        $currentTimestamp = (new DateTime("now",new DateTimeZone('UTC')))->getTimestamp();
        $privilegeExpiredTs = $currentTimestamp + $expireTimeInSeconds;

        //int用户id
        $token = RtcTokenBuilder::buildTokenWithUid($c['appID'],$c['appCertificate'],$i['channelName'],(int)$i['uid'],$role,$privilegeExpiredTs);

        //字符串账号
        //$token = RtcTokenBuilder::buildTokenWithUserAccount($c['appID'],$c['appCertificate'],self::$channelName,self::$uidStr,$role,$privilegeExpiredTs);

        return $token;

    }

    static public function RtmTokenBuilder($i){
        $c = ConfigModel::getKV("ShengWang");
        $validator = Validator::make(
            $i,
            [
                'uid' => "required",
            ],
            [
                'uid.required' => 'uid不能为空',
            ]
        );

        if(!empty($validator->errors()->first())){
            mdsResponse(400,$validator->errors()->first());
        }

        include_once(dirname(__FILE__) . "/src/RtmTokenBuilder.php");

        $user = $i['uid'];
        $role = RtmTokenBuilder::RoleRtmUser;
        $expireTimeInSeconds = 3600;
        $currentTimestamp = (new DateTime("now",new DateTimeZone('UTC')))->getTimestamp();
        $privilegeExpiredTs = $currentTimestamp + $expireTimeInSeconds;

        $token = RtmTokenBuilder::buildToken($c['appID'],$c['appCertificate'],$user,$role,$privilegeExpiredTs);

        return $token;

    }


}